package top.eggcode.template.log;

import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import top.eggcode.spring.AsyncExecutor;
import top.eggcode.system.client.LogClientService;
import top.eggcode.system.client.model.StorageLog;

/**
 * Title: 读写记录
 * Description: TODO
 * Date: 2021/5/25 9:45
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Order(10)
//@Aspect
@Component
@AllArgsConstructor
public class StorageRecordAspect {

    private final LogClientService logService;

    private final AsyncExecutor executor;

    @Pointcut("execution(public * top.eggcode..*.store.*.* (..))")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = point.proceed();
        long executionTime = System.currentTimeMillis() - start;
        String classPath = point.getSignature().getDeclaringTypeName();
        String methodName = point.getSignature().getName();

        StorageLog log = new StorageLog();

        log.setMethodName(methodName);
        log.setClassPath(classPath);
        log.setTimeSpent(executionTime);
        executor.commit(logService::saveToStorage, log);
        return proceed;
    }
}
